<?php
// +----------------------------------------------------------------------
// | 客管理系统签名验签

namespace  extendwechat;
use app\admin\model\course\ClassesConfig;
use app\admin\model\course\Order;
use app\admin\model\course\OrderGoods;
use app\admin\model\Third;
use app\admin\model\User;
use EasyWeChat\Factory;
use think\Exception;


class Message
{

    /*
     * 功能开通通知
     * */
    public function sendStudyMessage(){



        $config = get_addon_config('third');

        $today_start_time = strtotime(date("Y-m-d"));
//        $search_time = time() - $today_start_time + (3600*4);// 提前 4 小时
        $search_time = time() - $today_start_time; // 提前24 小时推送
//        exit;

        $w = date("w");
        if($w == 6){
            $w = -1;
        }
        $w  = $w + 1; //提前一天提醒  查询 明天的课程

        $w_arr = ClassesConfig::getWArr($w);
        $where["w"] = ["in",$w_arr];
        $app = Factory::officialAccount($config['wechatWeb']);
        OrderGoods::where("status",Order::STATUS_PAY)
//            ->where("id",5)
            ->where("start_time","<", $search_time)
            ->where("push_day","<>",date("Y-m-d"))
            ->where($where)
            ->chunk(100,function ($orderGoods) use ($app){
                foreach($orderGoods as $orderGood){
//                    dump($orderGood['id']);exit;
                    $unionid = Third::where("platform","wechatMiniApp")->where("user_id",$orderGood['user_id'])
                        ->value("unionid")??'';
                    $openid = Third::where("platform","wechatWeb")->where("unionid",$unionid)
                        ->value("openid")??'';
                    if($openid){
                        $a = $app->template_message->send([
                            'touser' => $openid,
                            'template_id' => 'DmmfGtobG6FiXsYo0EmGE7QSPTXqI7AQ9VOEqIikij8',
                            'miniprogram' => [
                                'appid' => 'wx27b477d1ffb878a4',
                                'pagepath' => 'pages/tabbar/index',
                            ],
                            'data' => [
                                'thing2' => config("site.name"),
                                'thing7' => $orderGood['student_name'],
                                'thing8' => $orderGood['course_name'],
//                                'time5' => $orderGood['time_name'],
                                'time5' => date("Y-m-d H:i",(strtotime(date("Y-m-d",strtotime("+1 days")))+$orderGood['start_time'])),
                                'thing9' => $orderGood['school_name'],
                            ],
                        ]);
//                        dump($a);
//                        exit;
                    }
                    $orderGood->save(["push_day"=>date("Y-m-d")]);

                }
            });




    }

    public function getFansList($next_openid = null,$fans_list = []){
        $config = get_addon_config('third');
//        dump($config['wechatWeb']);exit;
        $app = Factory::officialAccount($config['wechatWeb']);
        $list = $app->user->list($next_openid);

        if($list['count'] > 0){
            $fans_list = array_merge($list['data']['openid'], $fans_list);
            $this->getFansList($list['next_openid'], $fans_list);
        }
        return $fans_list;
    }

}